package com.heima.admin.job;

import com.heima.admin.service.WemediaNewsAutoScanService;
import com.heima.feigns.WemediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2021/8/12 11:37
 **/
@Component
@Slf4j
public class WeMediaNewsAutoScanJob {

    @Autowired
    WemediaFeign wemediaFeign;

    @Autowired
    WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @XxlJob("wemediaAutoScanJob")
    public ReturnT wemediaNewsAutoScanJob(String params){
        log.info("定时发布任务被触发  start==========================");
        // 1.远程调用wemedia查询待发布文章id列表
        ResponseResult<List<Integer>> releaseResult = wemediaFeign.findRelease();
        if (releaseResult.getCode().intValue()!=0) {
            log.error("远程调用 wemedia服务出现异常 , 原因:{}",releaseResult.getErrorMessage());
            return ReturnT.FAIL;
        }
        List<Integer> newsIdList = releaseResult.getData();
        if (CollectionUtils.isNotEmpty(newsIdList)) {
            for (Integer newsId : newsIdList) {
                // 2.调用自动审核发布方法  发布文章
                wemediaNewsAutoScanService.wemediaNewsAutoScanByMediaNewsId(newsId);
            }
        }
        log.info("定时发布任务被触发  end==========================");
        return ReturnT.SUCCESS;
    }
}
